package jp.eisbahn.mgapi.util;

/**
 * The utility class about a string operation.
 * @author Yoichiro Tanaka
 */
public final class StringUtils {

    /**
     * Initialize this instance.
     */
    private StringUtils() {
        super();
    }

    /**
     * Join each string.
     * @param array The array which has each string.
     * @param delimiter The character as the delimiter.
     * @return The joined string.
     */
    public static String join(final String[] array, final String delimiter) {
        if (array == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (int i = 0; i < array.length; i++) {
            if (first) {
                first = false;
            } else {
                sb.append(delimiter);
            }
            sb.append(array[i]);
        }
        return sb.toString();
    }

    /**
     * Create and retrieve an array which has a key and value.
     * @param key The key string.
     * @param value The value object.
     * @return The array of the key and value. If the value is null,
     * then return null.
     */
    public static String[] getKeyValueArray(
            final String key, final Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Integer) {
            int v = (Integer) value;
            if (v <= 0) {
                return null;
            }
        }
        return new String[] {key, value.toString()};
    }

}
